//
//  QRcodeReaderViewController.m
//  QRCodeReader
//
//  Created by razeen on 1/20/16.
//  Copyright © 2016 Razeen. All rights reserved.
//

#import "QRcodeReaderViewController.h"

@interface QRcodeReaderViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
- (IBAction)dismiss:(id)sender;
- (IBAction)SelectImage:(id)sender;

@end

@implementation QRcodeReaderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)dismiss:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)SelectImage:(id)sender {

    
    UIImagePickerController *pickerImage = [[UIImagePickerController alloc]init];
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        
        pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
        
    }

    pickerImage.delegate = self;
    pickerImage.allowsEditing = NO;
    
    [self presentViewController:pickerImage animated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    [self readQRCodeFromImageWithFileURL:image];

    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    
}

- (void)readQRCodeFromImageWithFileURL:(UIImage *)url
{
    // 根据URL找到CIImage
//    CIImage *image = [CIImage imageWithContentsOfURL:url];
    
    
    CIImage *image = [CIImage imageWithCGImage:url.CGImage];
    
    if (image)
    {
        // 创建CIDetector
        CIDetector *qrDetector = [CIDetector detectorOfType:CIDetectorTypeQRCode
                                                    context:[CIContext contextWithOptions:@{kCIContextUseSoftwareRenderer : @(YES)}]
                                                    options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];
        NSArray *features = [qrDetector featuresInImage:image];
        if ([features count] > 0)
        {
            for (CIFeature *feature in features)
            {
                if (![feature isKindOfClass:[CIQRCodeFeature class]])
                {
                    continue;
                }
                
                CIQRCodeFeature *qrFeature = (CIQRCodeFeature *)feature;
                NSString *code = qrFeature.messageString;
                NSLog(@"content is ：%@",code);
            }
        }
    }
    
    [self.extensionContext completeRequestReturningItems:nil
                                       completionHandler:^(BOOL expired) {
                                           NSLog(@"没有反应，关闭");
                                       }];
    
}

@end
